<?php

##########################################################################################################
Function valid_user_group($uid, $gid)
{
	$table_users_groups = $_SESSION['prefix']."users_groups";
	$req = dbquery("UPDATE $table_users_groups SET status='ok' WHERE uid='$uid' AND gid='$gid'");
	return (0);
}

##########################################################################################################
Function unvalid_user_group($uid, $gid)
{
	$table_users_groups = $_SESSION['prefix']."users_groups";
	$req = dbquery("DELETE FROM $table_users_groups WHERE (uid='$uid' AND gid='$gid') AND status='wait'");
	return (0);
}

##########################################################################################################
Function add_to_group($uid, $gid, $request="ok")
{			
	$table_users_groups = $_SESSION['prefix']."users_groups";
	
	$nid = create_mysql_id("$table_users_groups", "id");
	$req = dbquery("SELECT id FROM $table_users_groups WHERE uid='$uid' AND gid='$gid'");
	if (@mysql_num_rows($req) == 0)
		$req = dbquery("INSERT INTO $table_users_groups VALUES($nid, '$uid', '$gid', '$request')");
	return (0);
}

##########################################################################################################
Function rem_to_group($uid, $gid)
{	
	$table_users_groups = $_SESSION['prefix']."users_groups";
	
	$nid = create_mysql_id("$table_users_groups", "id");
	$req = dbquery("DELETE FROM $table_users_groups WHERE uid='$uid' AND gid='$gid'");
	return (0);
}

##########################################################################################################
function create_group($name, $description, $view="request")
{		
	if (trim($name) == "")
		return (-1);
	
	$table_groups = $_SESSION['prefix']."groups";
	
	$nid = create_mysql_id("$table_groups", "gid");
	$req = dbquery("INSERT INTO $table_groups VALUES($nid, '$name', '$description', '$view', '$_SESSION[uid]')");
	
	if (!$req)
		return (-1);	
	return (0);
}

################################################################################################################
Function edit_group($name, $description, $gid)
{
	if (!is_administrator($_SESSION['uid']))
	{
		if (!is_manager($_SESSION['uid']))
			return (-2);
	}
	
	if (($gid == 1) || ($gid == 2))
	{
		if (!is_root($_SESSION['login']))
			return (-2);
	}
	
	$table_groups = $_SESSION['prefix']."groups";
	
	$req = @dbquery("UPDATE $table_groups SET title='$name', description='$description' WHERE gid='$gid'");
	if (!$req)
		return (-1);
	return (0);
}

################################################################################################################
Function edit_delete_group($uids, $gid)
{
	if (!is_administrator($_SESSION['uid']))
		return (-2);
	
	if ((($gid == 2) || ($gid == 1)) && !is_root($_SESSION['login']))
		return (-2);
	
	$table_users_groups = $_SESSION['prefix']."users_groups";
	
	if (@$nb = count($uids)== 0)
		return (-1);
	    
	@$nb = count($uids);

	$uid = get_uid($_SESSION['login']);
	if (@$nb > 0)
	{
		$i = 0;
		$connection = dbconnect();
		while ($i < $nb)
		{ 
			$req = dbquery("DELETE FROM $table_users_groups WHERE gid='$gid' AND uid='$uids[$i]'", $connection);
			$i++;
		}
		mysql_close($connection);
	}
	return (0);
}

################################################################################################################
Function delete_group($gid)
{
	if (!is_administrator($_SESSION['uid']) || !is_manager($_SESSION['uid']) || ($gid == 1) || ($gid == 2))
		return (-1);
	
	$table_users_groups = $_SESSION['prefix']."users_groups";
	$table_groups = $_SESSION['prefix']."groups";
	
	$connection = dbconnect();
	$req = dbquery("DELETE FROM $table_groups WHERE gid='$gid'", $connection);
	$req = dbquery("DELETE FROM $table_users_groups WHERE gid='$gid'", $connection);
	@mysql_close($connection);
	return (0);
}

?>